const path = require('path');
const express = require('express');
const favicon = require('serve-favicon');
// const webpack = require('webpack');
const engine = require('ejs-locals');

// const webpackDevMiddleware = require('webpack-dev-middleware');
// const webpackHotMiddleware = require("webpack-Hot-middleware");
// const webpackConfig = require('./webpack.config');

const app = express(),
    DIST_DIR = path.join(__dirname, "dist"), // 设置静态访问文件路径
    PORT = 9000;/*,
    complier = webpack(webpackConfig);*/

app.engine('.html', engine);
app.set('views', './app/views/pages');
app.set('view engine', 'html');

require('./config/routes')(app);

app.use(favicon(path.join(__dirname, 'favicon.ico')));
app.use(express.static(DIST_DIR)); // 设置访问静态文件的路径

/*
app.listen(PORT, () => {
    console.log("成功启动：localhost:"+ PORT);
});
*/
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.use(function(socket, next){
    if (socket.request.headers.cookie) return next();
    next(new Error('Authentication error'));
});
io.on('connection', function(socket){
    // socket.send('连接成功');
    socket.emit('message', '连接成功');

    socket.on('message', function(data){
        console.log(data);
    });

    socket.on('disconnect', function(){
        console.log('断开连接');
    });

    // 全局广播
    socket.broadcast.emit('DATA', '欢迎使用');

    // 局部广播
    socket.broadcast.to('chat').emit('DATA', '今天天气晴');
});
io.of('chat').on('connection', function(socket){
    socket.emit('message', '欢迎进入【chat】room');
});
server.listen(PORT, () => {
    console.log("成功启动：localhost:" + PORT);
});